﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StockPredictor.Models;
using StockPredictor.Utilities;
using StockPredictor.App_Start;

namespace StockPredictor.Controllers
{
    public class UserPreferencesController : Controller
    {
        UserPreferencesRecordsContext userPreferencesContext = new UserPreferencesRecordsContext();
        UsersContext userContext = new UsersContext();

        #region Actions

        public ActionResult Index()
        {
            // get the userPreferences records specific to this user:
            if (User.Identity.IsAuthenticated)
            {
                // get the userPreferences information
                return View(UserPreferencesUtilities.GetUserPreferencesRecordsForUser(User.Identity.Name));
            }
            else
            {
                return RedirectToAction("Login", "Account");
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(UserPreferencesRecordModel record)
        {
            // update the record information with user identifying information
            record.UserId = AccountUtilities.GetUserIdForUser(User.Identity.Name);
            record.NotificationLastSentTime = DateTime.Now;
            userPreferencesContext.UserPreferencesRecords.Add(record);
            userPreferencesContext.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            var record = userPreferencesContext.UserPreferencesRecords.Find(id);
            if (record == null)
            {
                return HttpNotFound();
            }

            return View(record);
        }

        [HttpPost]
        public ActionResult Edit(UserPreferencesRecordModel record)
        {
            if (ModelState.IsValid)
            {
                userPreferencesContext.Entry(record).State = EntityState.Modified;
                userPreferencesContext.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(record);
        }

        #endregion Actions


    }
}
